import { NextRequest, NextResponse } from 'next/server'
import { db } from '@/lib/db'

export async function POST() {
  try {
    // 创建产品状态定义
    const states = [
      { name: 'DRAFT', description: '草稿状态', color: '#6B7280', order: 1 },
      { name: 'REVIEW', description: '审核中状态', color: '#F59E0B', order: 2 },
      { name: 'APPROVED', description: '已批准状态', color: '#3B82F6', order: 3 },
      { name: 'RELEASED', description: '已发布状态', color: '#10B981', order: 4 },
      { name: 'ARCHIVED', description: '已归档状态', color: '#EF4444', order: 5 }
    ]

    for (const state of states) {
      await db.productState.upsert({
        where: { name: state.name },
        update: state,
        create: state
      })
    }

    // 创建状态转换规则
    const transitions = [
      { fromState: 'DRAFT', toState: 'REVIEW', name: '提交审核', description: '提交产品进行审核', requireApproval: false },
      { fromState: 'DRAFT', toState: 'ARCHIVED', name: '直接归档', description: '将草稿状态的产品直接归档', requireApproval: false },
      { fromState: 'REVIEW', toState: 'APPROVED', name: '审核通过', description: '产品审核通过', requireApproval: true },
      { fromState: 'REVIEW', toState: 'DRAFT', name: '退回修改', description: '退回产品进行修改', requireApproval: false },
      { fromState: 'REVIEW', toState: 'ARCHIVED', name: '审核拒绝归档', description: '审核拒绝并归档', requireApproval: true },
      { fromState: 'APPROVED', toState: 'RELEASED', name: '发布产品', description: '发布已批准的产品', requireApproval: true },
      { fromState: 'APPROVED', toState: 'DRAFT', name: '重新编辑', description: '将已批准的产品退回编辑', requireApproval: true },
      { fromState: 'APPROVED', toState: 'ARCHIVED', name: '归档产品', description: '归档已批准的产品', requireApproval: true },
      { fromState: 'RELEASED', toState: 'ARCHIVED', name: '归档已发布产品', description: '归档已发布的产品', requireApproval: true },
      { fromState: 'ARCHIVED', toState: 'DRAFT', name: '重新激活', description: '重新激活已归档的产品', requireApproval: true }
    ]

    for (const transition of transitions) {
      await db.stateTransition.upsert({
        where: { 
          fromState_toState: {
            fromState: transition.fromState,
            toState: transition.toState
          }
        },
        update: transition,
        create: transition
      })
    }

    // 创建默认用户
    const defaultUsers = [
      { email: 'admin@example.com', name: '系统管理员' },
      { email: 'zhangsan@example.com', name: '张三' },
      { email: 'lisi@example.com', name: '李四' },
      { email: 'wangwu@example.com', name: '王五' }
    ]

    for (const user of defaultUsers) {
      await db.user.upsert({
        where: { email: user.email },
        update: user,
        create: user
      })
    }

    return NextResponse.json({ 
      message: 'Initialization completed successfully',
      states: states.length,
      transitions: transitions.length,
      users: defaultUsers.length
    })
  } catch (error) {
    console.error('Error initializing data:', error)
    return NextResponse.json(
      { error: 'Failed to initialize data' },
      { status: 500 }
    )
  }
}